package com.syni.mdd.sjb.community.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.WxPermanentCode;
import com.syni.mdd.sjb.common.component.mybatis.community.mapper.WxPermanentCodeMapper;
import com.syni.mdd.sjb.common.component.utils.redis.utils.RedisTemplateUtils;
import com.syni.mdd.sjb.community.base.constants.Constants;
import com.syni.mdd.sjb.community.base.service.WxCorpBusinessRelationService;
import com.syni.mdd.sjb.community.base.service.WxPermanentCodeService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lcc
 * @since 2020-07-21
 */
@Service
public class WxPermanentCodeServiceImpl extends ServiceImpl<WxPermanentCodeMapper, WxPermanentCode> implements WxPermanentCodeService {

    @Autowired
    WxCorpBusinessRelationService wxCorpBusinessRelationService;

    @Override
    public void addList(List<WxPermanentCode> wxPermanentCodeList) {
        if(CollectionUtils.isNotEmpty(wxPermanentCodeList)){
            wxPermanentCodeList.forEach(wc -> {
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq(WxPermanentCode.CORPID,wc.getCorpid());
                queryWrapper.eq(WxPermanentCode.SUITE_ID,wc.getSuiteId());
                WxPermanentCode wxPermanentCode = this.getOne(queryWrapper);
                if(wxPermanentCode == null){
                    this.save(wc);
                }else {
                    wxPermanentCode.setPermanentCode(wc.getPermanentCode());
                    wxPermanentCode.setResponseBody(wc.getResponseBody());
                    wxPermanentCode.setCorpid(wc.getCorpid());
                    wxPermanentCode.setEnterpriseName(wc.getEnterpriseName());
                    this.updateById(wxPermanentCode);
                }
                //删除token
                if(RedisTemplateUtils.hasKey(Constants.REDIS_KEY_ACCESS_TOKEN_BY_PCODE + wc.getCorpid())){
                    RedisTemplateUtils.delete(Constants.REDIS_KEY_ACCESS_TOKEN_BY_PCODE + wc.getCorpid());
                }
            });
            RedisTemplateUtils.getRedisTemplate().delete(Constants.REDIS_KEY_AUTH_CODE);
        }
    }

    @Override
    public String getPermanentCodeByCorpId(String corpId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(WxPermanentCode.CORPID,corpId);
        WxPermanentCode wxPermanentCode = this.getOne(queryWrapper);
        if(wxPermanentCode != null){
            return wxPermanentCode.getPermanentCode();
        }
        return null;
    }

    @Override
    public String getCropDataByCropId(String corpId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(WxPermanentCode.CORPID,corpId);
        WxPermanentCode wxPermanentCode = this.getOne(queryWrapper);
        if(wxPermanentCode != null){
            return wxPermanentCode.getResponseBody();
        }
        return null;
    }
}
